<?php

class Validate {

	static public function email($email) {

		$result = filter_var($email, FILTER_VALIDATE_EMAIL);

		if ($result != FALSE) {

			return new ValidationResult($result, true);
		} else {

			return new ValidationResult(NULL, false);
		}
	}

	static public function username($username) {

		$username = trim($username);
		$result = filter_var($username, FILTER_VALIDATE_REGEXP, array("options" => array("regexp" => "/^[a-zA-Z0-9_]{3,30}$/")));

		if ($result != FALSE) {

			return new ValidationResult($result, true);
		} else {

			return new ValidationResult(NULL, false);
		}
	}
	
	static public function name($name) {

		$name = trim($name);
		$result = filter_var($name, FILTER_VALIDATE_REGEXP, array("options" => array("regexp" => "/^.{3,30}$/")));

		if ($result != FALSE) {

			return new ValidationResult($result, true);
		} else {

			return new ValidationResult(NULL, false);
		}
	}

	static public function password($password) {

		$password = trim($password);
		$result = filter_var($password, FILTER_VALIDATE_REGEXP, array("options" => array("regexp" => "/^.{8,100}$/")));

		if ($result != FALSE) {

			return new ValidationResult($result, true);
		} else {

			return new ValidationResult(NULL, false);
		}
	}

	static public function noTags($text) {

		return strip_tags($text);

	}

	static public function someTags($text) {

		return strip_tags($text, '<p><b><i><trong><em><marquee>');

	}

	static public function pnr($pnr) {

		/*
		 * var_dump(filter_var( $string, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^M(.*)/"))))
		 * */
		$pnr = trim($pnr);

		//XXXXXX-XXXX 11, XXXXXXXX-XXXX 13, XXXXXXXXXX 10,  och XXXXXXXXXXXX 12

		echo "dess längd: " . strlen($pnr) . "<br>";

		$trimmedPnr = trim($pnr);

		//Längdcheck
		$pnrLength = strlen($trimmedPnr);
		if (!($pnrLength >= 10 && $pnrLength <= 13)) {

			//Fel längd
			echo "fel längd";
			return new ValidationResult(NULL, false);
		}

		if ($pnrLength == 11 || $pnrLength == 13) {

			$hyphen = substr($trimmedPnr, -5, 1);

			if ($hyphen == "-") {

				$trimmedPnr = str_replace($hyphen, "", $trimmedPnr);
			} else {

				//Bindestrecket fanns inte.
				echo "bindestrecket finns inte";
				return new ValidationResult(NULL, false);
			}

		}

		if (!is_numeric($trimmedPnr)) {

			//Inte bara siffror
			echo "Inte bara siffror<br>";
			return new ValidationResult(NULL, false);
		}

		$tenDigitPnr = substr($trimmedPnr, -10);

		if (!$this -> controlDigitIsValid($tenDigitPnr)) {
			echo "Fel kontrollsiffra<br>";
			return new ValidationResult(NULL, false);
		}

		echo substr($tenDigitPnr, 2, -4);
		if (!$this -> datePart(substr($tenDigitPnr, 2, -4))) {
			echo "fel datum<br>";
			return new ValidationResult(NULL, false);

		}
		echo "Rätt<br>";
		return new ValidationResult($tenDigitPnr, true);

		/*

		 if (filter_var($pnr, FILTER_VALIDATE_REGEXP, array("options" => array("regexp" => "/^[12]{1}[90]{1}[0-9]{6}-[0-9]{4}$/"))) != FALSE) {

		 return new ValidationResult($pnr, true);
		 } else {

		 return new ValidationResult(NULL, false);
		 }*/
	}

	static public function dateValidator($date) {

		$trimDate = trim($date);
		
	/*	if (!is_numeric($trimDate)) {
			echo "Inte bara nummer<br>";
			return new ValidationResult(NULL, false);
		}
		*/
		
		
		$length = strlen($date);
		//	if($length == 8 )

		$HyphenOne = substr($trimDate, -3, 1);

		$HyphenTwo = substr($trimDate, -6, 1);

		echo $HyphenOne . $HyphenTwo;

		if ($HyphenOne == "-" && $HyphenTwo == "-") {

			$hyphenStrippedDate = str_replace("-", "", $trimDate);
			
		} 
		else {
			echo "bindestrecket finns inte på rätt ställen";
			return new ValidationResult(NULL, false);
		}
		

		if (strlen($hyphenStrippedDate) == 6 || strlen($hyphenStrippedDate) == 8) {
					echo substr($hyphenStrippedDate, -4);
			$this->datePart(substr($hyphenStrippedDate, -4));
				
		}
		
		else {

			echo "Fel langd<br>";
			return new ValidationResult(NULL, false);
		}

		echo "yeah : " . $hyphenStrippedDate;
		return true;

	}

	static public function test() {

		echo '<br> <h2>email</h2>';
		$v = $this -> email("lk@dsf.se");
		print_r($v);

		echo '<br> <h2>username</h2>';
		$v = $this -> username("dsf");
		print_r($v);

		echo '<br> <h2>password</h2>';
		$v = $this -> password("dfsdsdsgg");
		print_r($v);

		echo '<br> <h2>noTags</h2>';
		$v = $this -> noTags("dfs<b><marquee>^<br>¨<br>|<br>--<br>|<br></marquee></b>sdfg");
		print_r($v);

		echo '<br> <h2>someTags</h2>';
		$v = $this -> someTags('dfs<b><marquee><marquee>-------<marquee>-------</marquee>-------</marquee></marquee></b>sdfg');
		print_r($v);

		echo '<br> <h2>personnummer</h2>';
		$v = $this -> pnr("198901184955");
		print_r($v);

		echo '<br> <h2>datePart (private)</h2>';
		$v = $this -> dateValidator("1912-12-03");
		print_r($v);

	}

	static private function controlDigitIsValid($number) {

		$count = 0;
		$length = strlen($number);

		for ($i = $length - 1; $i >= 0; --$i) {

			$count += 1;

			if ($count % 2) {

				$tmp = intval($number[$i]);

			} else {
				$tmp = intval($number[$i]) * 2;
				if ($tmp > 9) {
					$tmp = $tmp - 9;
				}
			}
			$sum += $tmp;
		}

		if ($sum % 10 == 0) {
			return true;
		}

		return false;
	}

	static private function datePart($date) {

		$month = substr($date, 0, 2);
		$day = substr($date, 2);
		$dayPerMonthArray = array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

		if ($month >= 1 && $month <= 12) {

			if (!($day >= 1 && $day <= $dayPerMonthArray[$month - 1])) {
				return FALSE;
			}
		} else {
			return FALSE;
		}
		return TRUE;
	}

}

class ValidationResult {

	private $value;
	private $isValid;
	
	public function getValue() {
		return $this->value;
	}
	
	public function isValid() {
		return $this->isValid;
	}

	public function __construct($value, $isValid) {

		$this -> value = $value;
		$this -> isValid = $isValid;

	}

}
